
#include "stdafx.h"
#include "CTopViewImageControl.h"
//#include <afxwin.h>
#include "CBoardCell2D.h"

#define BUTTON_TOP_VIEW_WIDTH		30

IMPLEMENT_DYNAMIC(CTopViewImageControl, CWnd)

BEGIN_MESSAGE_MAP(CTopViewImageControl, CWnd)
	ON_WM_PAINT()
    ON_WM_CREATE()
	ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()


CTopViewImageControl::CTopViewImageControl(void)
{
	m_pCellButtonLeft = 0;
	m_pCellButtonRight = 0;
}


CTopViewImageControl::~CTopViewImageControl(void)
{
	GdiplusShutdown(m_gdiplusToken);
	if ( m_pCellButtonLeft != 0) delete m_pCellButtonLeft;
	if ( m_pCellButtonRight != 0) delete m_pCellButtonRight;

}

int CTopViewImageControl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);

    return(0);
}

RECT CTopViewImageControl::CalTopViewImageControl( RECT rect )
{
	RECT rectTopView;
	rectTopView.bottom	= rect.top + this->m_nWidthControl;
	rectTopView.left	= rect.left;
	rectTopView.right	= rect.right;
	rectTopView.top		= rect.top;
	return rectTopView;
}
RECT CTopViewImageControl::CalButtonLeftWidth( RECT rect )
{
	RECT rectTopView;
	/*int width = 30;
	int height = 30;
*/
	int nCenterX = 0,  nCenterY = 0;
	nCenterX = ( rect.right - rect.left )/2;
	nCenterY = ( rect.bottom - rect.top )/2;

	rect.top  = rect.top +  nCenterY - BUTTON_TOP_VIEW_WIDTH/2;
	rect.right = rect.left +  BUTTON_TOP_VIEW_WIDTH;

	rectTopView.bottom	= rect.top + BUTTON_TOP_VIEW_WIDTH;
	rectTopView.left	= rect.left + 1;
	rectTopView.right	= rect.right;
	rectTopView.top		= rect.top;
	return rectTopView;
}
RECT CTopViewImageControl::CalButtonRightWidth( RECT rect )
{
	RECT rectTopView;
//	RECT rectTopView;
	/*int width = 30;
	int height = 30;
*/
	int nCenterX = 0,  nCenterY = 0;
	nCenterX = ( rect.right - rect.left )/2;
	nCenterY = ( rect.bottom - rect.top )/2;

	rect.top  = rect.top +  nCenterY - BUTTON_TOP_VIEW_WIDTH/2;
	rect.left  = rect.right - BUTTON_TOP_VIEW_WIDTH - 1;
	//rect.right = rect.left +  BUTTON_TOP_VIEW_WIDTH;

	rectTopView.bottom	= rect.top + BUTTON_TOP_VIEW_WIDTH;
	rectTopView.left	= rect.left;
	rectTopView.right	= rect.right;
	rectTopView.top		= rect.top;
	return rectTopView;
}
/*****************************************************************************
 * ++
 * Method name   :    void CTopViewImageControl::OnPaint(void)
 * Description   :    On event Paint handler. This function will be called when the WM_PAIN is created
 *
 * Parameters    : 
 * Return values :    
 * Mod. History  :    03/12/2012 - BaoTD1 (first created)
 *
 * --
 *****************************************************************************/
void CTopViewImageControl::OnPaint(void)
{
    CPaintDC dc(this); // Creates a device context for the client area, which
                   // also erases the area to be drawn.
    CRect rect;
    GetClientRect(rect);

    HDC Memhdc;
    HBITMAP Membitmap;
    
    // Double Buffer
    Memhdc = CreateCompatibleDC(dc.m_hDC);
    Membitmap = CreateCompatibleBitmap(dc.m_hDC, rect.Width() , rect.Height());
    CRect MemRect(0,0, rect.Width(), rect.Height());
    SelectObject(Memhdc, Membitmap);

    FillRect(Memhdc, MemRect, CreateSolidBrush(DEF_COLOR_YELLOW));
    Rectangle(Memhdc,MemRect.left, MemRect.top,MemRect.right, MemRect.bottom);
    // GDI+
    Graphics graphics(Memhdc);
    graphics.SetSmoothingMode(SmoothingModeHighQuality);

	/*RECT leftWidth, rightWidth;
	leftWidth = CalButtonLeftWidth( rect );
	DrawButtonLeft(&graphics, leftWidth );*/

    BitBlt(dc, rect.left, rect.top, rect.Width(), rect.Height(), Memhdc, 0, 0, SRCCOPY);
    DeleteObject(Membitmap);
    DeleteDC    (Memhdc);
    DeleteDC    (dc);
}

/******************************************************************************
* ++
* Method name   :    void CTopViewImageControl::DrawBoard2D(Graphics* graphics)
* Description   :    Draw Board 2D.
*
* Parameters    :  "graphic"    [in,out] If non-null, the graphic.
* Return values :    true if it succeeds, false if it fails.
* 
* Mod. History  :    03/12/2012 - BaoTD1 (first created)
*
* --
*****************************************************************************/

BOOL CTopViewImageControl::CreateViewImageControl(	int nWidthControl,	// width of control
													int nHeightControl,	// height of control
													int nNumOfImage,		// number of image
													RECT rect,				// rectangle of parent control
													CWnd* pParentWnd )		// handle of window parent
{
	int nID = 0;
	RECT rectTopView;

	 this->SetNumberOfImage( nNumOfImage );
	 this->SetSizeOfImageControl( nWidthControl, nHeightControl );

	rectTopView = CalTopViewImageControl( rect );

	///* Create window to draw board */
     if (!CWnd::Create(NULL, NULL, WS_VISIBLE, rectTopView, pParentWnd, nID, NULL))
    {
        throw LPCSTR("CTopViewImageControl::Create window fail !");
        return (FALSE);
    }
	if ( m_pCellButtonLeft  != 0) delete m_pCellButtonLeft;
	if ( m_pCellButtonRight != 0) delete m_pCellButtonRight;

	// Create button Left, right
	m_pCellButtonLeft  = new CBoardCell2D;
	m_pCellButtonRight = new CBoardCell2D;

	RECT leftWidth, rightWidth;
	leftWidth = CalButtonLeftWidth( rectTopView );
	m_pCellButtonLeft->CreateBoardCell( leftWidth, this );

	rightWidth = CalButtonRightWidth ( rectTopView );
	m_pCellButtonRight->CreateBoardCell(rightWidth, this );

	return TRUE;
}

//void CTopViewImageControl::LoadViewImage( CString pathFolderImage )
//{
//	m_strPathFolderImage = pathFolderImage;
//	FindImageInPath( pathFolderImage );
//
//}

void CTopViewImageControl::OnMouseMove(UINT nFlags, CPoint point)
{
	//BoradCellImage->ScrollWindow(-50,0);
//	ScrollWindow( -5, 0 );
}


void CTopViewImageControl::DrawButtonLeft( Graphics* graphics,  RECT rect )
{
   /* Gdiplus::Font myFont(L"Arial", 8);
    StringFormat format;
    format.SetAlignment(StringAlignmentCenter);*/
    SolidBrush blackBrush(Color(255, 0, 0, 0));
   
	// PenGray
	Pen PenGray(Color::Blue);

	SolidBrush ColorBrush(	Color::Blue );

	
			graphics->FillRectangle(	&ColorBrush,
										rect.left,
										rect.right,
										rect.bottom-rect.top, 
										rect.right -rect.left );

}
